﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MyChart
{
    /// <summary>
    /// 表示图表控件的多序列图表数据项集合
    /// </summary>
    public class MultiSeriesManager : ItemManager
    {
        #region 字段

        private ChartSeriesCollection _series = new ChartSeriesCollection();
        private List<string> _texts = new List<string>();

        #endregion

        #region 属性

        /// <summary>
        /// 获取一个 ChartSeriesCollection 对象集合，这些对象表示 FusionChart 控件中的当前的数据项序列集合
        /// </summary>
        public ChartSeriesCollection Series
        {
            get { return _series; }
        }

        /// <summary>
        /// 获取一个 List&lt;string&gt; 集合，表示数据项的文本集合（在坐标系图表中用来表示x坐标轴上的标签文本集合）
        /// </summary>
        public List<string> Texts
        {
            get { return _texts; }
        }

        /// <summary>
        /// 获取当前数据集合是否为空
        /// </summary>
        public override bool IsEmpty
        {
            get
            {
                if (_series.Count <= 0)
                {
                    return true;
                }
                else if (_series.Count == 1 && _series[0].Items.Count <= 0)
                {
                    return true;
                }
                /*else  //这里不用判断是否数据都为0，因为业务上即使都为0也要显示出图表来
                {
                    foreach (ChartSeries series in _series)
                    {
                        foreach (ChartItem item in series.Items)
                        {
                            double tmp;
                            if (double.TryParse(item.Value, out tmp))
                            {
                                if (tmp != 0)
                                {
                                    return false;
                                }
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }

                    return true;
                }*/

                return false;
            }
        }

        #endregion

        #region 构造器

        /// <summary>
        /// 初始化 MultiSeriesManager 类
        /// </summary>
        /// <param name="chart">关联数据项集合的控件实例</param>
        public MultiSeriesManager(FusionChart chart)
            : base(chart)
        {

        }

        #endregion

        #region 方法

        /// <summary>
        /// 获取当前集合对应的 FusionChart Xml配置字符串
        /// </summary>
        /// <returns>Xml配置字符串</returns>
        public override string GetXmlString()
        {
            StringBuilder xml = new StringBuilder();

            xml.Append("<categories>");
            foreach (string text in Texts)
            {
                xml.Append("<category label='" + text + "'/>");
            }
            xml.Append("</categories>");

            int iSeriesIndex = 0;
            base.DictArguments = new Dictionary<int, Dictionary<int, List<string>>>();

            double dItemValue = 0;

            foreach (ChartSeries s in Series)
            {
                xml.Append("<dataset seriesName='" + s.Name + "' showValues='" + (s.ShowValues && Chart.ShowValues ? 1 : 0)
                    + "' showAnchors='" + (s.ShowAnchors ? 1 : 0) + "' isDashed='" + (s.IsDashed ? 1 : 0) + "'"
                    + (Chart.ForeColor.ToArgb() != 0 ? " baseFontColor='" + Chart.ForeColor.ToArgb().ToString("X").Remove(0, 2) + "'" : "")
                    + " renderAs='" + s.RenderType.ToString().ToLower() + "'"
                    + (s.ParentYAxis == YAxis.RightYAxis ? " parentYAxis='S'" : "") + ">");

                int iItemIndex = 0;
                foreach (ChartItem item in s.Items)
                {
                    if (string.IsNullOrEmpty(item.Value))   //这里判断如果值为空或者null，那么更加业务需求，后续的item应该是不存在的，而不是自动填充0
                    {
                        break;
                    }

                    string toolText = "";
                    bool hasToolText = string.IsNullOrEmpty(s.ToolTextFormat) ? GetFormattedToolText(item.Row, out toolText) : GetFormattedToolText(s.ToolTextFormat, item.Row, out toolText);

                    string scriptText = string.Empty;
                    bool hasScriptText = string.IsNullOrEmpty(s.OnClientClick) ? GetFormattedScriptText(item.Row, iSeriesIndex, iItemIndex, out scriptText) : GetFormattedScriptText(s.OnClientClick, item.Row, iSeriesIndex, iItemIndex, out scriptText);

                    double.TryParse(item.Value, out dItemValue);

                    xml.Append("<set" + (hasScriptText ? " link='" + scriptText + "'" : "") + (hasToolText ? " toolText='" + toolText + "'" : "") + " value='" + (Chart.NumberSuffix == "%" && s.ParentYAxis == YAxis.LeftYAxis ? (dItemValue * 100).ToString("F" + this.Chart.Decimals) : dItemValue.ToString("F" + this.Chart.Decimals)) + "'/>");

                    iItemIndex++;
                }
                xml.Append("</dataset>");

                iSeriesIndex++;
            }
            return xml.ToString();
        }

        #endregion
    }
}
